package com.itasura.java8;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sailor wang
 * @date 2018/11/27 6:27 PM
 * @description
 */
public class Lambda {

    @Test
    public void forTest() {
        String[] atp = {"Rafael Nadal", "Novak Djokovic",
                "Stanislas Wawrinka",
                "David Ferrer", "Roger Federer",
                "Andy Murray", "Tomas Berdych",
                "Juan Martin Del Potro"};
        List<String> players = Arrays.asList(atp);
        // Java 8之前:
        for (String player : players) {
            System.out.print(player + "; ");
        }
        System.out.println("---------------------------------");
        // Java 8:使用 lambda 表达式以及函数操作(functional operation)
        players.forEach((player) -> System.out.print(player + "; "));
        System.out.println("---------------------------------");
        // 在 Java 8 中使用双冒号操作符(double colon operator)
        players.forEach(System.out::print);
    }

    @Test
    public void anonymousTest() {
        // Java 8之前:
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello world !");
            }
        }).start();

        //  Java 8方式:
        new Thread(() -> System.out.println("Hello world !")).start();

        // Java 8之前:
        Runnable race1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello world !");
            }
        };
        //  Java 8方式:
        Runnable race2 = () -> System.out.println("Hello world !");
        // 直接调用 run 方法
        race1.run();
        race2.run();
    }

    @Test
    public void sortTest() {
        String[] players = {"Rafael Nadal", "Novak Djokovic",
                "Stanislas Wawrinka", "David Ferrer",
                "Roger Federer", "Andy Murray",
                "Tomas Berdych", "Juan Martin Del Potro",
                "Richard Gasquet", "John Isner"};
        // Java 8之前:
        Arrays.sort(players, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return (s1.compareTo(s2));
            }
        });
        // Java 8方式:
        Comparator<String> sortByName = (String s1, String s2) -> (s1.compareTo(s2));
        Arrays.sort(players, sortByName);
        // Java 8方式:另一种形式
        Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2)));
    }


    @Test
    public void forEachTest() {
        Random random = new Random();
        random.ints().limit(10).forEach(System.out::println);
    }

    @Test
    public void mapTest() {
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        // 获取对应的平方数并去重
        List<Integer> squaresList = numbers.stream().map(i -> i * i)
                .distinct().collect(Collectors.toList());
        System.out.println(squaresList);
    }

    @Test
    public void filterTest() {
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        // 获取空字符串的数量
        long count = strings.stream().filter(string -> string.isEmpty()).count();
        System.out.println(count);
    }

    @Test
    public void limitTest() {
        Random random = new Random();
        random.ints().limit(8).forEach(System.out::println);
    }

    @Test
    public void sortedTest() {
        Random random = new Random();
        random.ints().limit(3).sorted().forEach(System.out::println);
    }

    @Test
    public void parallelTest() {
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        // 获取空字符串的数量
        long count = strings.parallelStream().filter(string -> string.isEmpty()).count();
        System.out.println("count -> {" + count + "}");
    }

    @Test
    public void collectorsTest() {
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println("筛选列表: " + filtered);
        String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
        System.out.println("合并字符串: " + mergedString);
    }

    @Test
    public void statisticsTest() {
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());
    }

}